#ifndef USBPRINTER_H
#define USBPRINTER_H

#include <QObject>
#include <QLibrary>
#include <QSerialPort>
#include <QSerialPortInfo>
#include "printerclient.h"

typedef int (*TSCabout)();
typedef int (*TSCopenport)(char*);
typedef int (*TSCsendcommand)(const char*);
typedef int (*TSCcloseport)();
typedef int (*TSCusbprinterquery)(char*);
typedef int (*TSCsendbinarydata)(const char*, int);

class UsbPrinter : public PrinterClient
{
    Q_OBJECT
public:
    explicit UsbPrinter(QObject *parent = nullptr);
    ~UsbPrinter();

    bool setPrinter(const QString &portName);

    bool connect();
    void disconnect();
    bool isConnected() const;

    bool sendCommand(const QByteArray &command);
    void sendData(const QByteArray &command);

private:
    QString portDescription(const QSerialPortInfo &port);
    int printerProbability(const QSerialPortInfo &port);
    bool isLikelyPrinter(const QSerialPortInfo &port);
    bool verifyPrinter();

signals:
    void connected();
    void disconnected();

private:
    QLibrary m_device;
    QString m_portName;
    quint16 m_vid;
    quint16 m_pid;

    TSCopenport openport;
    TSCsendcommand sendcommand;
    TSCsendbinarydata sendbinarydata;
    TSCcloseport closeport;
    TSCusbprinterquery usbprinterquery;

    // 通讯方法
    bool sendViaSerial(const QByteArray &data);
    bool sendViaUsb(const QByteArray &data);

    bool detectVirtualSerial();
    bool detectDirectUsb();
};

#endif // USBPRINTER_H
